package cn.bgs.controller;


import cn.bgs.common.R;
import cn.bgs.pojo.VehicleCar;
import cn.bgs.pojo.vo.VehicleCarVo;
import cn.bgs.service.IVehicleCarService;
import cn.bgs.vo.VehicleAdd;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import cn.bgs.common.R;
import cn.bgs.pojo.VehicleCar;
import cn.bgs.service.IVehicleCarService;
import cn.bgs.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 车辆表 前端控制器
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/vehicle-car")
public class VehicleCarController {
    @Autowired
    IVehicleCarService iVehicleCarService;
    @RequestMapping("/selectCarInfo")
    public R selectCarInfo(@RequestBody VehicleCar vehicleCar){
        return iVehicleCarService.selectCarInfo(vehicleCar);
    }

    @GetMapping("/deleteInfo")
    public R deleteInfo(Integer vehicleCarId){
        return iVehicleCarService.deleteInfo(vehicleCarId);
    }

    @Autowired
    private IVehicleCarService vehicleCarService;

    @PostMapping("/getMyCar/{userId}")
    @ApiOperation(value = "获取我的车辆")
    public R getMyCar(@PathVariable String userId){
        List<VehicleCar> vehicleCarList = vehicleCarService.getMyCar(userId);
        return R.ok().data("myCar",vehicleCarList);
    }

    @PostMapping("/addMyCar")
    @ApiOperation(value = "添加我的车辆")
    public R addMyCar(@RequestBody VehicleCarVo vehicleCarVo){
        String id = vehicleCarService.addMyCar(vehicleCarVo);
        if(id != null){
            return R.ok().message("添加成功");
        }else{
            return R.ok().message("添加失败");
        }
    }

    @PostMapping("/getOneCar/{carNumber}")
    @ApiOperation(value = "根据车牌号查询车辆")
    public R getOneCar(@PathVariable String carNumber){
        VehicleCar car = vehicleCarService.getOneCar(carNumber);
        return R.ok().data("car",car);
    }

    @PostMapping("/updCar")
    @ApiOperation(value = "修改车辆信息")
    public R updCar(@RequestBody VehicleCar vehicleCar){
        int upd = vehicleCarService.updCar(vehicleCar);
        if(upd == 1){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @PostMapping("/getAllCar")
    @ApiOperation(value = "获取全部车辆")
    public R getAllCar(){
        List<VehicleCar> carList = vehicleCarService.getAllCar();
        return R.ok().data("car",carList);

    }







    @GetMapping("/appFindVehicleCar/{userId}")
    @ApiOperation(value = "app我的车位(添加车位信息查询)")
    public R appFindVehicleCar(@PathVariable Integer userId){

        List<VehicleCar> vehicleCar = vehicleCarService.appFindVehicleCar(userId);
        if (vehicleCar != null){
            return R.ok().data("vehicleCar",vehicleCar);
        }else {
            return R.error();
        }
    }


    @PostMapping("/appudData")
    @ApiOperation(value = "app我的车位(车位信息添加修改)")
    public R appudData(@RequestBody VehicleAdd vehicleAdd){

        System.out.println("++++++"+vehicleAdd.toString());


        if (vehicleAdd.getUserId()!=0){

            vehicleCarService.appudData(vehicleAdd);

            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败，信息有误或者验证码失效");
        }

    }




    @PostMapping("/addVcarInfo")
    public R addVcarInfo(@RequestBody VehicleCar vehicleCar){
        boolean a = iVehicleCarService.addVcarInfo(vehicleCar);
        return R.ok().data("data",a);
    }

    @PostMapping("/updateInfo")
    public R updateInfo(@RequestBody VehicleCar vehicleCar){
        if(iVehicleCarService.updateInfo(vehicleCar)){
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("/infoPc")
    public R infoPc(UserVo userVo){
        return iVehicleCarService.infoPc(userVo);
    }
}
